﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace vJine.Lua.GUI.UI {
    /// <summary>
    /// WinParamTune.xaml 的交互逻辑
    /// </summary>
    public partial class WinParamTune : Window {
        public WinParamTune() {
            InitializeComponent();
        }

        public static void show_tune(LuaContext luaContext, string t, NodeConfig runConfig, NodeConfig currentNode) {

            WinParamTune wpt = new WinParamTune() { WindowStartupLocation = WindowStartupLocation.CenterScreen };
            wpt.title.Content = string.IsNullOrEmpty(t) ? "参数调节" : t;
            
            wpt.btnRun.Command = runConfig;
            Binding bindRunContent = new Binding() { Source = runConfig, Path = new PropertyPath("cmd_run") };
            wpt.btnRun.SetBinding(Button.ContentProperty, bindRunContent);
            Binding bindRunParam = new Binding() { Source = runConfig, Path = new PropertyPath("cmd_run_param") };
            wpt.btnRun.SetBinding(Button.CommandParameterProperty, bindRunParam);
            wpt.btnRun.Click += (object sender, RoutedEventArgs e) => {
                if (wpt.btnRun.CommandParameter as string != "retry") {
                    wpt.Close();
                }
            };

            wpt.luaParams.TYPE = LuaOptionType.W;
            wpt.luaParams.luaContext = luaContext;
            wpt.luaResults.TYPE = LuaOptionType.R;
            wpt.luaResults.luaContext = luaContext;
            wpt.luaProgs.TYPE = LuaOptionType.P;
            wpt.luaProgs.luaContext = luaContext;
            wpt.luaData.TYPE = LuaOptionType.D;
            wpt.luaData.luaContext = luaContext;

            NodeConfig configI = new NodeConfig();
            NodeConfig configO = new NodeConfig();
            NodeConfig configD = new NodeConfig();
            NodeConfig configP = new NodeConfig();

            if (currentNode.configs == null || currentNode.configs.Count == 0) {
                wpt.luaParams.set_bound(null);
                wpt.luaResults.set_bound(null);
                wpt.luaData.set_bound(null);
                wpt.luaProgs.set_bound(null);
            } else {
                for (int i = 0, len = currentNode.configs.Count; i < len; i++) {
                    NodeConfig config_i = currentNode.configs[i];
                    if (!config_i.w && !config_i.r && !config_i.data && !config_i.p) {
                        config_i.w = true;
                    }
                    if (config_i.w) {
                        configI.Add(config_i);
                    }
                    if (config_i.r) {
                        configO.Add(config_i);
                    }
                    if (config_i.data) {
                        configD.Add(config_i);
                    }
                    if (config_i.p) {
                        configP.Add(config_i);
                    }
                }

                wpt.luaParams.set_bound(configI);
                wpt.luaResults.set_bound(configO);
                wpt.luaData.set_bound(configD);
                wpt.luaProgs.set_bound(configP);
            }

            wpt.btnCancel.Command = runConfig;
            wpt.btnCancel.Click += (object sender, RoutedEventArgs e) => {
                wpt.Close();
            };
            wpt.btnIgnore.Command = runConfig;

            wpt.DataContext = currentNode;
            wpt.Dispatcher.BeginInvoke(new Action(()=>{
                wpt.ShowDialog();
                wpt = null;
            }));
        }
    }
}
